/**
 * jims
 */
package com.jims.master.modules.reg.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.reg.dao.ClinicScheduleDao;
import com.jims.master.modules.reg.entity.ClinicSchedule;
import com.jims.master.modules.reg.utils.DateUtil;
import com.jims.master.modules.reg.dao.ClinicForRegistDao;
import com.jims.master.modules.reg.entity.ClinicForRegist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Service
 * @author 刘方舟
 * @version 2017-02-27
 */
@Service
@Transactional(readOnly = true)
public class ClinicForRegistService extends CrudService<ClinicForRegistDao, ClinicForRegist> {
    @Autowired
	private ClinicForRegistDao clinicForRegistDao;
    @Autowired
    private ClinicScheduleDao clinicScheduleDao;
	public ClinicForRegist get(String id) {
		return super.get(id);
	}
	
	public List<ClinicForRegist> findList(ClinicForRegist clinicForRegist) {
		return super.findList(clinicForRegist);
	}
	
	public Page<ClinicForRegist> findPage(Page<ClinicForRegist> page, ClinicForRegist clinicForRegist) {
		return super.findPage(page, clinicForRegist);
	}
	
	@Transactional(readOnly = false)
	public int save(ClinicForRegist clinicForRegist) {
		return super.save(clinicForRegist);
	}
	
	@Transactional(readOnly = false)
	public Map<String,Object> delete(String ids) {
        Map<String,Object> map=new HashMap<String,Object>();
        int result=0;
        String info="";
        String[] idArr=ids.split(",");
        for(int i=1;i<=idArr.length;i++){
            ClinicForRegist clinicForRegist=super.get(idArr[i-1]);
            if(Integer.parseInt(clinicForRegist.getCurrentNum())>1){
                info=info+""+i+""+",";
                break;
            }
            result+=this.clinicForRegistDao.delReal(clinicForRegist.getId());
        }
        if(info.length()>0){
            info=info.substring(0,info.length()-1);
            map.put("info","第'"+info+"'条号表已有人挂号，不能删除！");
        }else{
            map.put("info","删除成功！");
        }
        if(result>0){
            map.put("result",true);
        }else{
            map.put("result",false);
        }
  		return map;
	}

	/**
	 * 查询当前日期的号表
	 * @return
	 * @author 赵宁
	 */
	public List<ClinicForRegist> findListToday(String clinicName,String deptId,String orgId){
		String date = DateUtils.getDate("yyyy-MM-dd");
       return  clinicForRegistDao.findRegistToday(date,clinicName,deptId,orgId);
	}

    /**
     * 生成号表
     * @param clinicScheduleIdsArr
     * @param startTime
     * @param endTime
     * @return
     */
    @Transactional(readOnly = false)
     public int regist(String[] clinicScheduleIdsArr, String startTime, String endTime) {
        //根据出诊安排的Ids 查询出诊安排
        List<ClinicSchedule> clinicScheduleList=new ArrayList<ClinicSchedule>();
        for(String clinicScheduleId:clinicScheduleIdsArr){
            clinicScheduleList.add(clinicScheduleDao.get(clinicScheduleId));
        }
        int num=1;
        if(clinicScheduleList!=null && clinicScheduleList.size()>0){
            ClinicSchedule clinicSchedule=new ClinicSchedule();
            for(int i=0;i<clinicScheduleList.size();i++){
                List<Integer> listWeek=new ArrayList<Integer>();
                clinicSchedule=clinicScheduleList.get(i);
                int ayOfWeek=Integer.parseInt(clinicSchedule.getDayOfWeek());
                if (ayOfWeek==7){
                    ayOfWeek=0;
                }
                listWeek.add(ayOfWeek);
                List<String> daysNeedBookList = DateUtil.getDates(startTime, endTime, listWeek);//根据门诊日期时间段 以及出诊时间查询出门诊日期
                String clinicDate="";
                for(int j=0;j<daysNeedBookList.size();j++){
                    ClinicForRegist clinicForRegist=new ClinicForRegist();
                    clinicDate=daysNeedBookList.get(j);
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    try {
                        clinicForRegist.setClinicDate(format.parse(clinicDate));//门诊日期
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    clinicForRegist.setCurrentRegisNum("0");
                    clinicForRegist.setTimeDesc(clinicSchedule.getTimeDesc());//出诊时间==门诊时间描述
                    clinicForRegist.setLimitPreNum(clinicSchedule.getLimitPreNum());
                    clinicForRegist.setLimitNum(clinicSchedule.getLimitNum());
                    clinicForRegist.setClinicIndexId(clinicSchedule.getClinicIndexId());
                    Integer currentNo=clinicForRegistDao.currentNoMax(clinicDate,clinicSchedule.getClinicIndexId(),clinicSchedule.getTimeDesc());
                    if(currentNo!=null){
                        clinicForRegist.setCurrentNum((currentNo + 1) + "");  //当前号(根据，门诊日期，门诊号名称，出诊时间 查询出最大的当前号数+1)
                    }else{
                        clinicForRegist.setCurrentNum("1");
                    }
                    clinicForRegist.setCurrentPreNum("0");
                    //保存前先判断 数据是否存在 （门诊日期，门诊号名称，出诊时间）
                    List<ClinicForRegist> regist= clinicForRegistDao.getClinicForRegist(clinicDate, clinicSchedule.getClinicIndexId(), clinicSchedule.getTimeDesc());
                    if(regist==null || regist.size()<=0){//
                        num=save(clinicForRegist);//
                    }
                }
            }
        }
        return num;
    }

    /**
     * <p>
     *     根据号别Id查询号表
     * </p>
     * @param clinicIndexId  号别Id
     * @return List<ClinicForRegist>
     */
    public List<ClinicForRegist> findbyClinicIndexId(String clinicIndexId) {
        return clinicForRegistDao.findbyClinicIndexId(clinicIndexId);
    }
}